import { Injectable } from '@nestjs/common';
import { CreateMenuDto } from './dto/create-menu.dto';
import { UpdateMenuDto } from './dto/update-menu.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Menus } from './entities/menu.entity';
import { Repository } from 'typeorm';

@Injectable()
export class MenusService {
    constructor(
        @InjectRepository(Menus) private menuRepository: Repository<Menus>,
    ) {}
    create(createMenuDto: CreateMenuDto) {
        // 创建menu实例
        const menu = this.menuRepository.create(createMenuDto)
        return this.menuRepository.save(menu);
    }

    findAll() {
        return this.menuRepository.find();
    }

    findOne(id: string) {
        return this.menuRepository.findOne({
            where:{id}
        });
    }

    async update(id: string, updateMenuDto: Menus) {
        const menu = await this.findOne(id)
        const newMenu = this.menuRepository.merge(menu , updateMenuDto)
        return this.menuRepository.save(newMenu);
    }

    remove(id: string) {
        return this.menuRepository.delete(id);
    }
}
